package com.whfc.openapi.api.fvs.service;

import com.whfc.common.entity.DefaultProperties;
import com.whfc.fvs.dto.FvsSettingDTO;
import com.whfc.fvs.enums.VsPlatform;
import com.whfc.fvs.service.FvsSettingService;
import com.whfc.openapi.redis.EzvizApiTokenRedisDao;
import com.whfc.openapi.redis.ImouApiTokenRedisDao;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 视频监控token获取
 * @author: qzexing
 * @version: 1.0
 * @date: 2020/5/6 14:38
 */
@Service
public class FvsTokenServiceImpl implements FvsTokenService {

    @Autowired
    private EzvizApiTokenRedisDao ezvizApiTokenRedisDao;

    @Autowired
    private ImouApiTokenRedisDao imouApiTokenRedisDao;

    @DubboReference(interfaceClass = FvsSettingService.class, version = "1.0.0")
    private FvsSettingService fvsSettingService;

    @Autowired
    private DefaultProperties defaultProps;

    @Override
    public String getEzvizToken(Integer deptId) {
        FvsSettingDTO appKeySecret = fvsSettingService.getAppKeySecret(deptId, VsPlatform.HIK_EZVIZ);
        if (appKeySecret == null) {
            appKeySecret = fvsSettingService.getAppKeySecret(defaultProps.getProjectId(), VsPlatform.HIK_EZVIZ);
            if (appKeySecret == null) {
                return "";
            }
        }
        return ezvizApiTokenRedisDao.getAccessToken(appKeySecret.getAppKey());
    }

    @Override
    public String getImouToken(Integer deptId) {
        FvsSettingDTO appKeySecret = fvsSettingService.getAppKeySecret(deptId, VsPlatform.DAHUA_IMOU);
        if (appKeySecret == null) {
            appKeySecret = fvsSettingService.getAppKeySecret(defaultProps.getProjectId(), VsPlatform.DAHUA_IMOU);
            if (appKeySecret == null) {
                return "";
            }
        }
        return imouApiTokenRedisDao.getAccessToken(appKeySecret.getAppKey());
    }
}
